import 'package:flutter/material.dart';

enum StageTheme { elementary, middle, high }

enum AccentPalette { vibrant, neon, candy }

ThemeData buildAppTheme() {
  // 默认使用“初中”主题
  return buildAppThemeForStage(StageTheme.middle);
}

ThemeData buildAppThemeForStage(
  StageTheme stage, {
  AccentPalette palette = AccentPalette.vibrant,
}) {
  final Color seedColor = switch (stage) {
    StageTheme.elementary => Colors.lightBlue,
    StageTheme.middle => Colors.indigo,
    StageTheme.high => Colors.blueGrey,
  };

  // 基础字体缩放比例（设备自适应将在运行时计算）
  final double scale = switch (stage) {
    StageTheme.elementary => 0.85, // 小学：基础缩放
    StageTheme.middle => 0.80, // 初中：基础缩放
    StageTheme.high => 0.75, // 高中：基础缩放
  };

  // Accent palette tweak
  final Color accent = switch (palette) {
    AccentPalette.vibrant => seedColor,
    AccentPalette.neon => Colors.cyanAccent.shade400,
    AccentPalette.candy => Colors.pinkAccent.shade200,
  };

  final ColorScheme colorScheme = ColorScheme.fromSeed(
    seedColor: accent,
    brightness: Brightness.light,
  );

  TextStyle _scaled(TextStyle base) =>
      base.copyWith(fontSize: (base.fontSize ?? 14) * scale);

  final TextTheme baseTextTheme = Typography.blackMountainView.copyWith(
    titleLarge: const TextStyle(fontSize: 18, fontWeight: FontWeight.w700),
    titleMedium: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
    titleSmall: const TextStyle(fontSize: 14, fontWeight: FontWeight.w600),
    bodyLarge: const TextStyle(fontSize: 14, height: 1.35),
    bodyMedium: const TextStyle(fontSize: 13, height: 1.35),
    bodySmall: const TextStyle(fontSize: 11, height: 1.35),
    labelLarge: const TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
  );

  final TextTheme textTheme = baseTextTheme.copyWith(
    titleLarge: _scaled(baseTextTheme.titleLarge!),
    titleMedium: _scaled(baseTextTheme.titleMedium!),
    titleSmall: _scaled(baseTextTheme.titleSmall!),
    bodyLarge: _scaled(baseTextTheme.bodyLarge!),
    bodyMedium: _scaled(baseTextTheme.bodyMedium!),
    bodySmall: _scaled(baseTextTheme.bodySmall!),
    labelLarge: _scaled(baseTextTheme.labelLarge!),
  );

  // 针对平板优化的图标尺寸
  final double iconSize = switch (stage) {
    StageTheme.elementary => 20,
    StageTheme.middle => 18,
    StageTheme.high => 16,
  };

  return ThemeData(
    colorScheme: colorScheme,
    useMaterial3: true,
    visualDensity: VisualDensity.adaptivePlatformDensity,
    scaffoldBackgroundColor: colorScheme.surface,
    textTheme: textTheme,
    materialTapTargetSize: MaterialTapTargetSize.padded,
    // 高对比度与护眼预设（简化实现）：根据 palette 调整对比与背景微色调
    brightness: Brightness.light,

    appBarTheme: AppBarTheme(
      backgroundColor: colorScheme.surface,
      foregroundColor: colorScheme.onSurface,
      elevation: 0,
      centerTitle: true,
      titleTextStyle: textTheme.titleMedium?.copyWith(
        color: colorScheme.onSurface,
      ),
    ),

    iconTheme: IconThemeData(size: iconSize, color: colorScheme.onSurface),
    primaryIconTheme: IconThemeData(
      size: iconSize,
      color: colorScheme.onPrimary,
    ),

    cardTheme: CardTheme(
      color: colorScheme.surfaceContainerHighest,
      elevation: 0,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
      margin: EdgeInsets.zero,
    ),

    snackBarTheme: SnackBarThemeData(
      behavior: SnackBarBehavior.floating,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
    ),

    inputDecorationTheme: InputDecorationTheme(
      filled: true,
      fillColor: colorScheme.surfaceContainerHighest,
      hintStyle: TextStyle(color: colorScheme.onSurface.withOpacity(0.6)),
      contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(14),
        borderSide: BorderSide.none,
      ),
      enabledBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(14),
        borderSide: BorderSide.none,
      ),
      focusedBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(14),
        borderSide: BorderSide(color: colorScheme.primary, width: 1.2),
      ),
      errorBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(14),
        borderSide: const BorderSide(color: Colors.red),
      ),
    ),

    filledButtonTheme: FilledButtonThemeData(
      style: FilledButton.styleFrom(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
        textStyle: textTheme.labelLarge,
      ),
    ),
    outlinedButtonTheme: OutlinedButtonThemeData(
      style: OutlinedButton.styleFrom(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
        textStyle: textTheme.labelLarge,
      ),
    ),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
        textStyle: textTheme.labelLarge,
      ),
    ),

    chipTheme: ChipThemeData(
      backgroundColor: colorScheme.surfaceContainerHighest,
      selectedColor: colorScheme.primaryContainer,
      labelStyle: TextStyle(color: colorScheme.onSurface),
      secondaryLabelStyle: TextStyle(color: colorScheme.primary),
      padding: EdgeInsets.symmetric(
        horizontal: 10 * scale,
        vertical: 6 * scale,
      ), // 响应式padding
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(20 * scale),
      ), // 响应式圆角
    ),

    tabBarTheme: TabBarTheme(
      labelColor: colorScheme.primary,
      unselectedLabelColor: colorScheme.onSurface.withOpacity(0.6),
      labelStyle: textTheme.bodyLarge?.copyWith(fontWeight: FontWeight.w700),
      unselectedLabelStyle: textTheme.bodyLarge?.copyWith(
        fontWeight: FontWeight.w500,
      ),
      indicator: BoxDecoration(
        color: colorScheme.primaryContainer,
        borderRadius: BorderRadius.circular(8 * scale), // 响应式圆角
      ),
      dividerColor: Colors.transparent,
    ),

    listTileTheme: ListTileThemeData(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12 * scale),
      ), // 响应式圆角
      tileColor: colorScheme.surface,
      iconColor: colorScheme.onSurface,
      contentPadding: EdgeInsets.symmetric(
        horizontal: 12 * scale,
        vertical: 4 * scale,
      ), // 响应式padding
    ),

    floatingActionButtonTheme: FloatingActionButtonThemeData(
      backgroundColor: colorScheme.primary,
      foregroundColor: colorScheme.onPrimary,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
    ),

    dividerTheme: DividerThemeData(
      color: colorScheme.onSurface.withOpacity(0.08),
      thickness: 1,
    ),

    bottomSheetTheme: BottomSheetThemeData(
      backgroundColor: colorScheme.surface,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
      ),
      showDragHandle: true,
      dragHandleColor: colorScheme.onSurface.withOpacity(0.2),
    ),
  );
}
